package entities;

import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import util.ApplicationConstant;

import com.activity.MainAppActivity;
import com.activity.MapActivity;
import com.activity.R;
import com.fragment.MainFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import dao.LoadCategory;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Locate implements Comparable<Locate>,Serializable {
	/**
	 * 
	 */
	private int id;
	//image represent for locate item on listview
	private ArrayList<Bitmap> images;
	private Boolean isChecked;
	private double log,la;
	private String name,address,phone,website,email,categoryId,infor;
	private ArrayList<String> imageNames;
	private  String dateAdded;
	public  Boolean SHOW_INFOR_WINDOW=false;
	public Boolean IS_POINT_DIRECTION=false;
	public static String patternParse=" ";
	public Locate(Locate locate) {
		super();
		this.id = locate.id;
		this.images =new ArrayList<Bitmap>(locate.images);
		this.isChecked = locate.isChecked;
		this.log = locate.log;
		this.la = locate.la;
		this.name = locate.name;
		this.address = locate.address;
		this.phone = locate.phone;
		this.website = locate.website;
		this.email = locate.email;
		this.categoryId = locate.categoryId;
		this.infor = locate.infor;
		this.imageNames = new ArrayList<String>(locate.getImageNames());
		this.dateAdded = locate.dateAdded;
	}

	public String getDateAdded() {
		return dateAdded;
	}
	public void setDateAdded(String dateAdded) {
		this.dateAdded = dateAdded;
	}
	public Locate(){
		imageNames=new ArrayList<String>();
		images=new ArrayList<Bitmap>();
		isChecked=false;
	}
	public Boolean getIsChecked() {
		return isChecked;
	}

	public void setIsChecked(Boolean isChecked) {
		this.isChecked = isChecked;
	}
	public ArrayList<Bitmap> getImages() {
		return images;
	}

	public void setImages(ArrayList<Bitmap> images) {
		this.images = images;
	}

	public int getId() {
		return id;
	}
	public ArrayList<String> getImageNames() {
		return imageNames;
	}
	public void setImageNames(ArrayList<String> images) {
		this.imageNames = images;
	}
	public void setId(int id) {
		this.id = id;
	}
	public double getLog() {
		return log;
	}
	public void setLog(double log) {
		this.log = log;
	}
	public double getLa() {
		return la;
	}
	public void setLa(double la) {
		this.la = la;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAdress() {
		return address;
	}
	public void setAdress(String adress) {
		this.address = adress;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getWebsite() {
		return website;
	}
	public void setWebsite(String website) {
		this.website = website;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
	public String getInfor() {
		return infor;
	}
	public void setInfor(String infor) {
		this.infor = infor;
	}
	/*
	 * chua lam
	 */
	public void markOnMap()
	{
		MarkerOptions marker=new MarkerOptions();
		LatLng latlong=new LatLng(la,log);
		marker.position(latlong);
		marker.title(name+"  "+getDistance()+MainAppActivity.unitDistance);
		marker.snippet(address);
		if(IS_POINT_DIRECTION)
			marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.mylocate));
		else
			marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_ic));
		
		if(SHOW_INFOR_WINDOW)
			MapActivity.googleMap.addMarker(marker).showInfoWindow();		
		else 
			MapActivity.googleMap.addMarker(marker);		
	}
	public void moveCamera(){
		LatLng latlog=new LatLng(la, log);
		MapActivity.googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlog));
		MapActivity.googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
	}
	// ham tinh khoang cach
	public double getDistance()
	{
		LatLng locate=MainAppActivity.orginalLatl;
		double theta=locate.longitude-log;
		double dist=Math.sin(deg2rad(locate.latitude)) * Math.sin(deg2rad(la))
				+ Math.cos(deg2rad(locate.latitude)) * Math.cos(deg2rad(la)) * Math.cos(deg2rad(theta));
		dist=Math.acos(dist);
		dist=rad2deg(dist);
		dist=dist * 60; 
		dist=dist * 1852; 
		dist/=1000.0;
		dist*=100;
		dist=Math.round(dist)/100.0;
		return dist;
	}	
	//tinh khoang cach toi 1 diem
	public double getDistanceFromLocation(LatLng locate)
	{
		double theta=locate.longitude-log;
		double dist=Math.sin(deg2rad(locate.latitude)) * Math.sin(deg2rad(la))
				+ Math.cos(deg2rad(locate.latitude)) * Math.cos(deg2rad(la)) * Math.cos(deg2rad(theta));
		dist=Math.acos(dist);
		dist=rad2deg(dist);
		dist=dist * 60; 
		dist=dist * 1852; 
		dist/=1000.0;
		dist*=100;
		dist=Math.round(dist)/100.0;
		return dist;
	}

	// doi tu radian sang do
	private double rad2deg(double rad) {
		return rad * 180.0/Math.PI;
	}
	
	// doi tu do sang radian
	private double deg2rad(double deg) {
		return (deg*Math.PI/180.0);
	}
	@Override
	public int compareTo(Locate another) {
		// TODO Auto-generated method stub
		if(getDistance()>another.getDistance())
			return 1;
		if(getDistance()<another.getDistance())
			return -1;
		return 0;
	}	
	public Bitmap getDefaultImage(){
		List<Category> listCategories=MainFragment.listCategory;
		Log.d("---getDefaultImage:list category size:", ""+listCategories.size());
		if(listCategories!=null&&listCategories.size()>0){
			for(Category category:listCategories){
				if(category.getCategoryId().equals(categoryId))
				{
					Log.d("getDefaultImage: found caid:", categoryId);
					return category.getBitmap();
				}
			}
		}
		return null;
	}
	public void loadItemImage(final Handler handler){
		//save on images[0]
		class Loader extends AsyncTask<ArrayList<String>, Void, ArrayList<Bitmap>>{

			
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
			}
			@Override
			protected void onPostExecute(ArrayList<Bitmap> result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
			}
			@Override
			protected ArrayList<Bitmap> doInBackground(ArrayList<String>... imgNames) {
				// TODO Auto-generated method stub
				if(imageNames.size()>0){
					Log.d("Loading item images","..........");
					Bitmap mIcon11=null;
					String urldisplay = ApplicationConstant.URL_IMAGES
							+ imageNames.get(0);
					try {
						InputStream in = new java.net.URL(
								urldisplay).openStream();
						mIcon11 = BitmapFactory.decodeStream(in);
						if(mIcon11!=null)
							images.add(mIcon11);
					} catch (Exception e) {
						Log.d("default", "-------");
						Log.e("Error", e.getMessage());
						e.printStackTrace();
					}
				}
				else
				{
					Log.d("default img","-----------------");
					images.add(getDefaultImage());
				}
				handler.sendEmptyMessage(1);
				return null;
			}
		}
		if(images.size()==0){
			Loader loader=new Loader();
			loader.execute();
		}
	}
	public void LoadAllImages(final Handler handler,ProgressDialog progressDialog){
		class LoadAllImages extends AsyncTask<ArrayList<String>, Void, ArrayList<Bitmap>>{
			private ProgressDialog progressDialog;
			public LoadAllImages(ProgressDialog progressDialog){
				this.progressDialog=progressDialog;
			}
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
				progressDialog.setCancelable(false);
				progressDialog.setIndeterminate(true);
				progressDialog.show();
			}
			@Override
			protected void onPostExecute(ArrayList<Bitmap> result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				if (progressDialog != null&&progressDialog.isShowing())
					progressDialog.dismiss();
				handler.sendMessage(new Message());
			}
			@Override
			protected ArrayList<Bitmap> doInBackground(ArrayList<String>... agr) {
				// TODO Auto-generated method stub
				Log.d("Loading all images","..........");
				int k=0;
				if(images.size()==1)
					k=1;
				for(int i=k;i<agr[0].size();i++){
						Bitmap mIcon11=null;
						String urldisplay = ApplicationConstant.URL_IMAGES
								+agr[0].get(i);
						try {
							InputStream in = new java.net.URL(
									urldisplay).openStream();
							mIcon11 = BitmapFactory.decodeStream(in);
							getImages().add(mIcon11);
						} catch (Exception e) {
							Log.d("default", "-------");
							Log.e("Error", e.getMessage());
							e.printStackTrace();
						}
				}
				return null;
			}
		}
		LoadAllImages loadAllImages=new LoadAllImages(progressDialog);
		loadAllImages.execute(imageNames);
	}
}
